﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SwinGame;

namespace SimpleSurvival.src
{
    //-----------------------------------------------//
    //CLASS - EntityDrawer.cs
    //-----------------------------------------------//
    //Draws a given IEntity
    public class EntityDrawer
    {
        //Entity Bitmaps
        List<SwinGame.Bitmap> _entityBitmaps = new List<SwinGame.Bitmap>();

        //Draws all entitys in list that are visible on screen.
        public void DrawEntityList(List<IEntity> iel, ICamera c, EntityDefinitions defs)
        {
            int lx = c.getLeftBounds();
            int rx = c.getRightBounds();
            int ty = c.getTopBounds();
            int by = c.getBottomBounds();

            for (int i = 0; i < iel.Count; i++)
            {
                if (iel[i].X < rx && iel[i].X > lx - 32)    //x index within camera width
                {
                    if (iel[i].Y < by && iel[i].Y > ty - 32)    //y index within camera height
                    {                                           //really speeds up render speeds
                        DrawEntity(iel[i], c, defs);
                    }
                }
            }
        }

        void DrawEntity(IEntity e, ICamera cam, EntityDefinitions defs)
        {
            Vector pos = cam.GetRelativePosition(new Vector { X = e.X, Y = e.Y });
            _entityBitmaps[defs.GetEntityDefId(e.Name)].Draw(pos);
        }

        //Returns the bitmap of an entity
        public SwinGame.Bitmap GetEntityTypeBitmap(String name, EntityDefinitions defs)
        {
            return _entityBitmaps[defs.GetEntityDefId(name)];
        }

        public EntityDrawer(EntityDefinitions defs)
        {
            int ids = defs.EntityDefIdCount;                         
            for (int i = 0; i < ids; i++)
            {
                //if hasnt been added
                string path = defs.GetEntityDefValue(i, EntityDefinitions.FieldName.ENTITY_IMAGE_PATH);
                bool added = false;
                SwinGame.Bitmap nb = null;
                foreach (SwinGame.Bitmap b in _entityBitmaps) { if (b.Filename == path) { added = true; nb = b ; } }
                if (nb != null) { _entityBitmaps.Add(nb); }

                if (!added)
                _entityBitmaps.Add( new SwinGame.Bitmap( defs.GetEntityDefValue(i, EntityDefinitions.FieldName.ENTITY_IMAGE_PATH)));
            }
        }

    }
}
